#include <stdio.h>
#include <string.h>
#include "uart_protocol.h"

// 模拟串口发送函数
void uart_send_data(uint8_t *data, uint8_t length)
{
    printf("发送数据: ");
    for (int i = 0; i < length; i++) {
        printf("%02X ", data[i]);
    }
    printf("\n");
}

// 命令处理回调函数
void command_handler(uint8_t command, uint8_t *data, uint8_t length)
{
    printf("收到命令: 0x%02X, 数据长度: %d\n", command, length);
    
    if (length > 0) {
        printf("数据内容: ");
        for (int i = 0; i < length; i++) {
            printf("%02X ", data[i]);
        }
        printf("\n");
    }
    
    // 根据命令类型处理
    switch (command) {
        case CMD_UV_CONTROL:
            printf("UV控制命令: %s\n", data[0] == STATUS_ON ? "开启" : "关闭");
            break;
            
        case CMD_HYDROGEN_CONTROL:
            printf("制氢控制命令: %s\n", data[0] == STATUS_ON ? "开启" : "关闭");
            break;
            
        case CMD_LIGHT_COLOR:
            printf("灯光颜色命令: ");
            switch (data[0]) {
                case LIGHT_COLOR_RED:
                    printf("红色\n");
                    break;
                case LIGHT_COLOR_YELLOW:
                    printf("黄色\n");
                    break;
                case LIGHT_COLOR_BLUE:
                    printf("蓝色\n");
                    break;
                case LIGHT_COLOR_PURPLE:
                    printf("紫色\n");
                    break;
                case LIGHT_COLOR_GREEN:
                    printf("绿色\n");
                    break;
                case LIGHT_COLOR_CYAN:
                    printf("青色\n");
                    break;
                case LIGHT_COLOR_WHITE:
                    printf("白色\n");
                    break;
                default:
                    printf("未知颜色: %d\n", data[0]);
                    break;
            }
            break;
            
        case CMD_LIGHT_MODE:
            printf("灯光模式命令: ");
            switch (data[0]) {
                case LIGHT_MODE_OFF:
                    printf("关闭\n");
                    break;
                case LIGHT_MODE_CONSTANT:
                    printf("常亮\n");
                    break;
                case LIGHT_MODE_BREATHING:
                    printf("呼吸\n");
                    break;
                case LIGHT_MODE_RAINBOW:
                    printf("七彩渐变\n");
                    break;
                default:
                    printf("未知模式: %d\n", data[0]);
                    break;
            }
            break;
            
        case CMD_BUZZER_CONTROL:
            if (data[0] == 0) {
                printf("蜂鸣器关闭\n");
            } else {
                printf("蜂鸣器控制: %d声\n", data[0]);
            }
            break;
            
        case CMD_CLEAN_CONTROL:
            printf("清洗控制命令: %s\n", data[0] == STATUS_ON ? "开启" : "关闭");
            break;
            
        case CMD_SHUTDOWN:
            printf("关机命令\n");
            break;
            
        case CMD_BACKLIGHT_CONTROL:
            printf("背光控制命令: %s\n", data[0] == STATUS_ON ? "开启" : "关闭");
            break;
            
        case CMD_CHARGE_STATUS:
            printf("充电状态上报: ");
            switch (data[0]) {
                case CHARGE_STATUS_NONE:
                    printf("未充电\n");
                    break;
                case CHARGE_STATUS_CHARGING:
                    printf("充电中\n");
                    break;
                case CHARGE_STATUS_COMPLETE:
                    printf("已充满\n");
                    break;
                default:
                    printf("未知状态: %d\n", data[0]);
                    break;
            }
            break;
            
        case CMD_HEARTBEAT:
            printf("心跳包\n");
            break;
            
        default:
            printf("未知命令\n");
            break;
    }
}

// 测试接收和解析数据
void test_receive_data(uint8_t *data, uint8_t length)
{
    static protocol_parser_t parser;
    static bool parser_initialized = false;
    
    if (!parser_initialized) {
        protocol_init(&parser);
        parser_initialized = true;
    }
    
    printf("接收数据: ");
    for (int i = 0; i < length; i++) {
        printf("%02X ", data[i]);
    }
    printf("\n");
    
    for (int i = 0; i < length; i++) {
        if (protocol_parse_byte(&parser, data[i])) {
            if (protocol_validate_frame(&parser.frame)) {
                command_handler(parser.frame.command, parser.frame.data, parser.frame.length);
            } else {
                printf("帧校验失败\n");
            }
        }
    }
    
    printf("\n");
}

// 测试发送命令
void test_send_commands()
{
    printf("===== 测试发送命令 =====\n");
    
    // 测试UV控制命令
    printf("\n[测试] 开UV灯\n");
    esp32_send_uv_control(STATUS_ON);
    
    printf("\n[测试] 关UV灯\n");
    esp32_send_uv_control(STATUS_OFF);
    
    // 测试制氢控制命令
    printf("\n[测试] 开制氢\n");
    esp32_send_hydrogen_control(STATUS_ON);
    
    printf("\n[测试] 关制氢\n");
    esp32_send_hydrogen_control(STATUS_OFF);
    
    // 测试灯光颜色命令
    printf("\n[测试] 灯光颜色-红灯\n");
    esp32_send_light_color(LIGHT_COLOR_RED);
    
    printf("\n[测试] 灯光颜色-黄灯\n");
    esp32_send_light_color(LIGHT_COLOR_YELLOW);
    
    printf("\n[测试] 灯光颜色-蓝灯\n");
    esp32_send_light_color(LIGHT_COLOR_BLUE);
    
    printf("\n[测试] 灯光颜色-紫灯\n");
    esp32_send_light_color(LIGHT_COLOR_PURPLE);
    
    printf("\n[测试] 灯光颜色-绿灯\n");
    esp32_send_light_color(LIGHT_COLOR_GREEN);
    
    printf("\n[测试] 灯光颜色-青灯\n");
    esp32_send_light_color(LIGHT_COLOR_CYAN);
    
    printf("\n[测试] 灯光颜色-白灯\n");
    esp32_send_light_color(LIGHT_COLOR_WHITE);
    
    // 测试灯光模式命令
    printf("\n[测试] 灯光模式-关闭\n");
    esp32_send_light_mode(LIGHT_MODE_OFF);
    
    printf("\n[测试] 灯光模式-常亮\n");
    esp32_send_light_mode(LIGHT_MODE_CONSTANT);
    
    printf("\n[测试] 灯光模式-呼吸\n");
    esp32_send_light_mode(LIGHT_MODE_BREATHING);
    
    printf("\n[测试] 灯光模式-七彩渐变\n");
    esp32_send_light_mode(LIGHT_MODE_RAINBOW);
    
    // 测试蜂鸣器控制命令
    printf("\n[测试] 蜂鸣器一声\n");
    esp32_send_buzzer_control(0x01);
    
    printf("\n[测试] 蜂鸣器255声\n");
    esp32_send_buzzer_control(0xFF);
    
    printf("\n[测试] 关蜂鸣器\n");
    esp32_send_buzzer_control(0x00);
    
    // 测试清洗控制命令
    printf("\n[测试] 开清洗\n");
    esp32_send_clean_control(STATUS_ON);
    
    printf("\n[测试] 关清洗\n");
    esp32_send_clean_control(STATUS_OFF);
    
    // 测试关机命令
    printf("\n[测试] 关机\n");
    esp32_send_shutdown();
    
    // 测试背光控制命令
    printf("\n[测试] 开背光\n");
    esp32_send_backlight_control(STATUS_ON);
    
    printf("\n[测试] 关背光\n");
    esp32_send_backlight_control(STATUS_OFF);
}

// 测试接收命令
void test_receive_commands()
{
    printf("===== 测试接收命令 =====\n");
    
    // 测试UV控制命令
    uint8_t cmd_uv_on[] = {0xAA, 0x55, 0x01, 0x10, 0x01, 0x10, 0x0D, 0x0A};
    printf("\n[测试] 接收开UV灯命令\n");
    test_receive_data(cmd_uv_on, sizeof(cmd_uv_on));
    
    uint8_t cmd_uv_off[] = {0xAA, 0x55, 0x01, 0x10, 0x00, 0x11, 0x0D, 0x0A};
    printf("\n[测试] 接收关UV灯命令\n");
    test_receive_data(cmd_uv_off, sizeof(cmd_uv_off));
    
    // 测试制氢控制命令
    uint8_t cmd_hydrogen_on[] = {0xAA, 0x55, 0x01, 0x11, 0x01, 0x11, 0x0D, 0x0A};
    printf("\n[测试] 接收开制氢命令\n");
    test_receive_data(cmd_hydrogen_on, sizeof(cmd_hydrogen_on));
    
    uint8_t cmd_hydrogen_off[] = {0xAA, 0x55, 0x01, 0x11, 0x00, 0x10, 0x0D, 0x0A};
    printf("\n[测试] 接收关制氢命令\n");
    test_receive_data(cmd_hydrogen_off, sizeof(cmd_hydrogen_off));
    
    // 测试灯光颜色命令
    uint8_t cmd_light_red[] = {0xAA, 0x55, 0x01, 0x12, 0x01, 0x12, 0x0D, 0x0A};
    printf("\n[测试] 接收灯光颜色-红灯命令\n");
    test_receive_data(cmd_light_red, sizeof(cmd_light_red));
    
    uint8_t cmd_light_yellow[] = {0xAA, 0x55, 0x01, 0x12, 0x02, 0x11, 0x0D, 0x0A};
    printf("\n[测试] 接收灯光颜色-黄灯命令\n");
    test_receive_data(cmd_light_yellow, sizeof(cmd_light_yellow));
    
    uint8_t cmd_light_blue[] = {0xAA, 0x55, 0x01, 0x12, 0x03, 0x10, 0x0D, 0x0A};
    printf("\n[测试] 接收灯光颜色-蓝灯命令\n");
    test_receive_data(cmd_light_blue, sizeof(cmd_light_blue));
    
    uint8_t cmd_light_purple[] = {0xAA, 0x55, 0x01, 0x12, 0x04, 0x17, 0x0D, 0x0A};
    printf("\n[测试] 接收灯光颜色-紫灯命令\n");
    test_receive_data(cmd_light_purple, sizeof(cmd_light_purple));
    
    uint8_t cmd_light_green[] = {0xAA, 0x55, 0x01, 0x12, 0x05, 0x16, 0x0D, 0x0A};
    printf("\n[测试] 接收灯光颜色-绿灯命令\n");
    test_receive_data(cmd_light_green, sizeof(cmd_light_green));
    
    uint8_t cmd_light_cyan[] = {0xAA, 0x55, 0x01, 0x12, 0x06, 0x15, 0x0D, 0x0A};
    printf("\n[测试] 接收灯光颜色-青灯命令\n");
    test_receive_data(cmd_light_cyan, sizeof(cmd_light_cyan));
    
    uint8_t cmd_light_white[] = {0xAA, 0x55, 0x01, 0x12, 0x07, 0x14, 0x0D, 0x0A};
    printf("\n[测试] 接收灯光颜色-白灯命令\n");
    test_receive_data(cmd_light_white, sizeof(cmd_light_white));
    
    // 测试灯光模式命令
    uint8_t cmd_light_mode_off[] = {0xAA, 0x55, 0x01, 0x13, 0x00, 0x12, 0x0D, 0x0A};
    printf("\n[测试] 接收灯光模式-关闭命令\n");
    test_receive_data(cmd_light_mode_off, sizeof(cmd_light_mode_off));
    
    uint8_t cmd_light_mode_constant[] = {0xAA, 0x55, 0x01, 0x13, 0x01, 0x13, 0x0D, 0x0A};
    printf("\n[测试] 接收灯光模式-常亮命令\n");
    test_receive_data(cmd_light_mode_constant, sizeof(cmd_light_mode_constant));
    
    uint8_t cmd_light_mode_breathing[] = {0xAA, 0x55, 0x01, 0x13, 0x02, 0x10, 0x0D, 0x0A};
    printf("\n[测试] 接收灯光模式-呼吸命令\n");
    test_receive_data(cmd_light_mode_breathing, sizeof(cmd_light_mode_breathing));
    
    uint8_t cmd_light_mode_rainbow[] = {0xAA, 0x55, 0x01, 0x13, 0x03, 0x11, 0x0D, 0x0A};
    printf("\n[测试] 接收灯光模式-七彩渐变命令\n");
    test_receive_data(cmd_light_mode_rainbow, sizeof(cmd_light_mode_rainbow));
    
    // 测试蜂鸣器控制命令
    uint8_t cmd_buzzer_once[] = {0xAA, 0x55, 0x01, 0x14, 0x01, 0x14, 0x0D, 0x0A};
    printf("\n[测试] 接收蜂鸣器一声命令\n");
    test_receive_data(cmd_buzzer_once, sizeof(cmd_buzzer_once));
    
    uint8_t cmd_buzzer_many[] = {0xAA, 0x55, 0x01, 0x14, 0xFF, 0xEA, 0x0D, 0x0A};
    printf("\n[测试] 接收蜂鸣器255声命令\n");
    test_receive_data(cmd_buzzer_many, sizeof(cmd_buzzer_many));
    
    uint8_t cmd_buzzer_off[] = {0xAA, 0x55, 0x01, 0x14, 0x00, 0x15, 0x0D, 0x0A};
    printf("\n[测试] 接收关蜂鸣器命令\n");
    test_receive_data(cmd_buzzer_off, sizeof(cmd_buzzer_off));
    
    // 测试清洗控制命令
    uint8_t cmd_clean_on[] = {0xAA, 0x55, 0x01, 0x15, 0x01, 0x15, 0x0D, 0x0A};
    printf("\n[测试] 接收开清洗命令\n");
    test_receive_data(cmd_clean_on, sizeof(cmd_clean_on));
    
    uint8_t cmd_clean_off[] = {0xAA, 0x55, 0x01, 0x15, 0x00, 0x14, 0x0D, 0x0A};
    printf("\n[测试] 接收关清洗命令\n");
    test_receive_data(cmd_clean_off, sizeof(cmd_clean_off));
    
    // 测试关机命令
    uint8_t cmd_shutdown[] = {0xAA, 0x55, 0x01, 0x16, 0x00, 0x17, 0x0D, 0x0A};
    printf("\n[测试] 接收关机命令\n");
    test_receive_data(cmd_shutdown, sizeof(cmd_shutdown));
    
    // 测试背光控制命令
    uint8_t cmd_backlight_on[] = {0xAA, 0x55, 0x01, 0x17, 0x01, 0x17, 0x0D, 0x0A};
    printf("\n[测试] 接收开背光命令\n");
    test_receive_data(cmd_backlight_on, sizeof(cmd_backlight_on));
    
    uint8_t cmd_backlight_off[] = {0xAA, 0x55, 0x01, 0x17, 0x00, 0x16, 0x0D, 0x0A};
    printf("\n[测试] 接收关背光命令\n");
    test_receive_data(cmd_backlight_off, sizeof(cmd_backlight_off));
    
    // 测试充电状态上报
    uint8_t cmd_charge_none[] = {0xAA, 0x55, 0x01, 0x20, 0x00, 0x21, 0x0D, 0x0A};
    printf("\n[测试] 接收未充电上报\n");
    test_receive_data(cmd_charge_none, sizeof(cmd_charge_none));
    
    uint8_t cmd_charge_charging[] = {0xAA, 0x55, 0x01, 0x20, 0x01, 0x20, 0x0D, 0x0A};
    printf("\n[测试] 接收充电中上报\n");
    test_receive_data(cmd_charge_charging, sizeof(cmd_charge_charging));
    
    uint8_t cmd_charge_complete[] = {0xAA, 0x55, 0x01, 0x20, 0x02, 0x23, 0x0D, 0x0A};
    printf("\n[测试] 接收已充满上报\n");
    test_receive_data(cmd_charge_complete, sizeof(cmd_charge_complete));
    
    // 测试心跳包
    uint8_t cmd_heartbeat[] = {0xAA, 0x55, 0x01, 0x31, 0x00, 0x30, 0x0D, 0x0A};
    printf("\n[测试] 接收心跳包\n");
    test_receive_data(cmd_heartbeat, sizeof(cmd_heartbeat));
}

int main()
{
    printf("===== UART通讯协议测试程序 =====\n\n");
    
    // 测试发送命令
    test_send_commands();
    
    // 测试接收命令
    test_receive_commands();
    
    return 0;
} 